package com.googlecode.cswish.oosearch;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Iterator;

import org.apache.commons.lang.StringUtils;

/**
 * @author Jerry.Feng Date: 2005-2-27 22:14:28
 * @version $id: $
 */
public class QLInfo implements Serializable {
	
	private String ql;
	private List<String> paramNames;
	private List<Object> params;
	private int firstResult;
	private int maxResults;
	private String sort;
	private String dir = "ASC";
	
	private boolean withPermissionFilter;

	public QLInfo() {
	}
	
	public QLInfo(String ql, Object ... params) {
		this.ql = ql;
		if (params != null) {
			this.params = Arrays.asList(params);
		}
	}
	
	public QLInfo(String ql, List<Object> params) {
		this.ql = ql;
		this.params = params;
	}
	
	public int getFirstResult() {
		return firstResult;
	}
	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
	}
	public String getQl() {
		return ql;
	}
	public void setQl(String ql) {
		this.ql = ql;
	}
	public List<String> getParamNames() {
		return paramNames;
	}

	public void setParamNames(List<String> paramNames) {
		this.paramNames = paramNames;
	}

	public List<Object> getParams() {
		return params;
	}
	
	public void setParams(List<Object> qlParams) {
		this.params = qlParams;
	}
	
	public int getMaxResults() {
		return maxResults;
	}
	
	public void setMaxResults(int maxResults) {
		this.maxResults = maxResults;
	}
	
	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		if (dir != null) {
			this.dir = dir;
		}
	}

	public boolean isWithPermissionFilter() {
		return withPermissionFilter;
	}

	public void setWithPermissionFilter(boolean withPermissionFilter) {
		this.withPermissionFilter = withPermissionFilter;
	}

	/**
	 * replace '?' in hql using the params
	 * @return
	 */
	public String toString() {
		if (ql == null) return null;

		if (params == null || params.size() == 0) {
			return ql;
		}

		StringBuffer hqlBuf = new StringBuffer(ql.length() + params.size() * 10);
		int index = 0;
		int lastIndex = 0;
		Iterator<Object> paramsIter = params.iterator();
		while(index >= 0 && paramsIter.hasNext()) {
			lastIndex = index;
			index = ql.indexOf('?', lastIndex);
			if (index >= 0) {
				hqlBuf.append(ql.substring(lastIndex, index));
				index++;
			} else {
				hqlBuf.append(ql.substring(lastIndex));
			}

			// FIXME: the ? isn't match to hqlParams
			Object param = paramsIter.next();
			String value = param == null ? "" : param.toString();
			// replace the unsafe char,
            // NOTICE:it's only valid in oracle db
            // if you use it in other db, please repair this.
			value = StringUtils.replace(value, "'", "''");

			// remove it because the HqlParser convert the object to string
			//boolean isStringType;
			//if (param instanceof String) {
			//	isStringType = true;
				hqlBuf.append('\'');
			//} else {
			//	isStringType = false;
			//}
			hqlBuf.append(value);
			//if (isStringType) {
				hqlBuf.append('\'');
			//}
		}
		if (index > 0) {
			hqlBuf.append(ql.substring(index));
		}

		return hqlBuf.toString();
	}
}